<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Custom Clickable Objects</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Custom Clickable Objects</p>
<hr class="separator">
<div class="content"><img src="images/customclickable_doc.png" width='607' height='367'><br><br>
In this example, a clickable line chart will be created with legend keys, a title box, a vertical mark with label, and a custom text box. All these objects will be clickable in addition to the lines and the data points. This example also demonstrate how to use Javascript as the click handler.<br><br>
<b>[JSP Version]</b> jspdemo/customclick.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// The data for the line chart
double[] data0 = {50, 55, 47, 36, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73, 77,
    84, 82, 80, 84};
double[] data1 = {36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24, 28, 36, 30, 45, 46, 42, 48, 45,
    43, 52, 64, 70};

// The labels for the line chart
String[] labels = {"Jan-04", "Feb-04", "Mar-04", "Apr-04", "May-04", "Jun-04", "Jul-04", "Aug-04",
    "Sep-04", "Oct-04", "Nov-04", "Dec-04", "Jan-05", "Feb-05", "Mar-05", "Apr-05", "May-05",
    "Jun-05", "Jul-05", "Aug-05", "Sep-05", "Oct-05", "Nov-05", "Dec-05"};

// Create an XYChart object of size 600 x 360 pixels, with a light blue (EEEEFF) background, black
// border, 1 pxiel 3D border effect and rounded corners
XYChart c = new XYChart(600, 360, 0xeeeeff, 0x000000, 1);
c.setRoundedFrame();

// Set plotarea at (55, 60) with size of 520 x 240 pixels. Use white (ffffff) as background and grey
// (cccccc) for grid lines
c.setPlotArea(55, 60, 520, 240, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);

// Add a legend box at (55, 58) (top of plot area) using 9pt Arial Bold font with horizontal layout
// Set border and background colors of the legend box to Transparent
LegendBox legendBox = c.addLegend(55, 58, false, "Arial Bold", 9);
legendBox.setBackground(Chart.Transparent);

// Reserve 10% margin at the top of the plot area during auto-scaling to leave space for the
// legends.
c.yAxis().setAutoScale(0.1);

// Add a title to the chart using 15pt Times Bold Italic font. The text is white (ffffff) on a blue
// (0000cc) background, with glass effect.
TextBox title = c.addTitle("Monthly Revenue for Year 2000/2001", "Times New Roman Bold Italic", 15,
    0xffffff);
title.setBackground(0x0000cc, 0x000000, Chart.glassEffect(Chart.ReducedGlare));

// Add a title to the y axis
c.yAxis().setTitle("Month Revenue (USD millions)");

// Set the labels on the x axis. Draw the labels vertical (angle = 90)
c.xAxis().setLabels(labels).setFontAngle(90);

// Add a vertical mark at x = 17 using a semi-transparent purple (809933ff) color and Arial Bold
// font. Attached the mark (and therefore its label) to the top x axis.
Mark mark = c.xAxis2().addMark(17, 0x809933ff, "Merge with Star Tech", "Arial Bold");

// Set the mark line width to 2 pixels
mark.setLineWidth(2);

// Set the mark label font color to purple (0x9933ff)
mark.setFontColor(0x9933ff);

// Add a copyright message at (575, 295) (bottom right of plot area) using Arial Bold font
TextBox copyRight = c.addText(575, 295, "(c) Copyright Space Travel Ltd", "Arial Bold");
copyRight.setAlignment(Chart.BottomRight);

// Add a line layer to the chart
LineLayer layer = c.addLineLayer();

// Set the default line width to 3 pixels
layer.setLineWidth(3);

// Add the data sets to the line layer
layer.addDataSet(data0, -1, "Enterprise");
layer.addDataSet(data1, -1, "Consumer");

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Create an image map for the chart
String chartImageMap = c.getHTMLImageMap("xystub.jsp", "",
    "title='{dataSetName} @ {xLabel} = USD {value|0} millions'");

// Create an image map for the legend box
String legendImageMap = legendBox.getHTMLImageMap(
    "javascript:popMsg('the legend key [{dataSetName}]');", " ",
    "title='This legend key is clickable!'");

// Obtain the image map for the title
String titleImageMap = "&lt;area " + title.getImageCoor() +
    " href='javascript:popMsg(\"the chart title\");' title='The title is clickable!'&gt;";

// Obtain the image map for the mark
String markImageMap = "&lt;area " + mark.getImageCoor() +
    " href='javascript:popMsg(\"the Merge with Star Tech mark\");' title='The Merge with Star " +
    "Tech text is clickable!'&gt;";

// Obtain the image map for the copyright message
String copyRightImageMap = "&lt;area " + copyRight.getImageCoor() +
    " href='javascript:popMsg(\"the copyright message\");' title='The copyright text is " +
    "clickable!'&gt;";

// Get the combined image map
String imageMap = chartImageMap + legendImageMap + titleImageMap + markImageMap + copyRightImageMap;

// Assign the image map to the chart
viewer.setImageMap(imageMap);
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Custom Clickable Objects&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
&lt;script type="text/javascript"&gt;
function popMsg(msg) {
    alert("You have clicked on " + msg + ".");
}
&lt;/script&gt;
&lt;div style="font:bold 18pt verdana;"&gt;
    Custom Clickable Objects
&lt;/div&gt;
&lt;hr style="border:solid 1px #000080" /&gt;
&lt;div style="font:10pt verdana; margin-bottom:20px"&gt;
    &lt;a href='viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;'&gt;View Source Code&lt;/a&gt;
&lt;/div&gt;
&lt;div style="font:10pt verdana; width:600px; margin-bottom:20px"&gt;
    In the following chart, the lines, legend keys, title, copyright, and the "Merge
    with Star Tech" text are all clickable!
&lt;/div&gt;
&lt;!-- ****** Here is the chart image ****** --&gt;
&lt;%=viewer.renderHTML(response)%&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div><br>
The above code is quite similar to most of the sample codes in this documentation, so it will not be discussed in detail. The main difference is the code for creating the image map.<br><br>
The image map in this example consists of multiple parts. The part for the line chart is produced using <a href="BaseChart.getHTMLImageMap.htm">BaseChart.getHTMLImageMap</a> with "xystub.jsp" as the handler:<br><br>
<div class="codeblock"><code class='pre'>String chartImageMap = c.getHTMLImageMap("xystub.jsp", "",
    "title='{dataSetName} @ {xLabel} = USD {value|0} millions'");</code></div><br>
For demo purpose, the handler simple displays information on what is clicked.<br><br>
<b>[JSP Version]</b> jspdemo/xystub.jsp
<div class='codeblock'><code class='pre'>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable XY Chart Handler&lt;/title&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
&lt;div style="font:bold 18pt verdana;"&gt;
    Simple Clickable XY Chart Handler
&lt;/div&gt;
&lt;hr style="border:solid 1px #000080" /&gt;
&lt;div style="font:10pt verdana; margin-bottom:20px"&gt;
    &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
&lt;/div&gt;
&lt;div style="font:10pt verdana;"&gt;
&lt;b&gt;You have clicked on the following chart element :&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Data Set : &lt;%=request.getParameter("dataSetName")%&gt;&lt;/li&gt;
    &lt;li&gt;X Position : &lt;%=request.getParameter("x")%&gt;&lt;/li&gt;
    &lt;li&gt;X Label : &lt;%=request.getParameter("xLabel")%&gt;&lt;/li&gt;
    &lt;li&gt;Data Value : &lt;%=request.getParameter("value")%&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div><br>
The image map for the legend keys is produced using <a href="LegendBox.getHTMLImageMap.htm">LegendBox.getHTMLImageMap</a>. In this example, Javascript will be executed when the user clicks on the legend key.<br><br>
<div class="codeblock"><code class='pre'>String legendImageMap = legendBox.getHTMLImageMap(
    "javascript:popMsg('the legend key [{dataSetName}]');", " ",
    "title='This legend key is clickable!'");</code></div><br>
As shown above, the "javascript:popMsg(...);" as used in the url parameter of "getHTMLImageMap". The browser will interpret it as a Javascript statement to be executed when the hot spot is clicked.<br><br>
Note that the second argument to <a href="LegendBox.getHTMLImageMap.htm">LegendBox.getHTMLImageMap</a> is set to a space character. This argument specifies the HTTP query parameters to be appended to the url. In this example, as the url is actually Javascript, no query parameter is necessary, so a space character is used. (We cannot use an empty string, as it means default query parameters, not no query parameter.)<br><br>
The Javascript executed in this example just pops up a message.<br><br>
<div class="codeblock"><code class='pre'>&lt;script type="text/javascript"&gt;
function popMsg(msg) {
    alert("You have clicked on " + msg + ".");
}
&lt;/script&gt;</code></div><br>
Most text messages in ChartDirector are represented as <a href="TextBox.htm">TextBox</a> objects. Their image map coordinates can be retrieved using <a href="Box.getImageCoor.htm">Box.getImageCoor</a>. This allows &lt;AREA&gt; tags for TextBox objects be constructed easily. This example illustrates how to use this method to create image maps for the chart title, mark label, and the copyright message.<br><br>
<div class="codeblock"><code class='pre'>String titleCoor = title.getImageCoor();
String markCoor = mark.getImageCoor();
String copyRightCoor = copyRight.getImageCoor();</code></div><br>
All the image map strings are simply appended together to form the image map for the chart.<br><br>
<div class="codeblock"><code class='pre'>string imageMap = chartImageMap + legendImageMap + titleImageMap + markImageMap + copyRightImageMap;</code></div></div><br>
<hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div></body>
</HTML>
